﻿using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;

namespace BF3StatsNetApi.BLL
{
    public class Stat
    {
        private DateTime? _date_insert;
        public object date_insert { 
            get { return _date_insert; } 
            set {
                _date_insert = Utility.UnixTimeStampToDateTime(value);
            } 
        }
        private DateTime? _date_update;
        public object date_update { 
            get { return _date_update; } 
            set {
                _date_update = Utility.UnixTimeStampToDateTime(value);
            } 
        }
        private DateTime? _date_check;
        public object date_check { 
            get { return _date_check; } 
            set {
                _date_check = Utility.UnixTimeStampToDateTime(value);
            } 
        }

        public string checkstate { get; set; }
        public Score scores { get; set; }
        public Global global { get; set; }
        public Coop coop { get; set; }

        public Rank rank { get; set; }
        private List<Rank> _nextranks;
        public object nextranks { 
            get {
                return _nextranks;
            } 
            set {
                _nextranks = new List<Rank>();
                foreach (JToken nr in (JArray)value)
                {
                    Rank _rank = JsonConvert.DeserializeObject<Rank>(nr.ToString());
                    _nextranks.Add(_rank);
                }
            } 
        }

        private List<Team> _teams;
        public object teams { 
            get { return _teams; } 
            set {
                _teams = new List<Team>();
                foreach (KeyValuePair<string, JToken> t in (JObject)value)
                {
                    Team _team = JsonConvert.DeserializeObject<Team>(t.Value.ToString());
                    _teams.Add(_team);
                }
            } 
        }

        private List<Weapon> _weapons;
        public object weapons { 
            get { return _weapons; } 
            set {
                _weapons = new List<Weapon>();
                foreach (KeyValuePair<string, JToken> w in (JObject)value)
                {
                    Weapon _weapon = JsonConvert.DeserializeObject<Weapon>(w.Value.ToString());
                    _weapons.Add(_weapon);
                }
            } 
        }

        private List<Equipment> _equipment;
        public object equipment { 
            get { return _equipment; } 
            set {
                _equipment = new List<Equipment>();
                foreach (KeyValuePair<string, JToken> eq in (JObject)value)
                {
                    Equipment _equip = JsonConvert.DeserializeObject<Equipment>(eq.Value.ToString());
                    _equipment.Add(_equip);
                }
            } 
        }

        private List<Vehicle> _vehicles;
        public object vehicles { 
            get { return _vehicles; } 
            set {
                _vehicles = new List<Vehicle>();
                foreach (KeyValuePair<string, JToken> v in (JObject)value)
                {
                    Vehicle _vehicle = JsonConvert.DeserializeObject<Vehicle>(v.Value.ToString());
                    _vehicles.Add(_vehicle);
                }
            } 
        }

        private List<Ribbon> _ribbons;
        public object ribbons { 
            get { return _ribbons; } 
            set {
                _ribbons = new List<Ribbon>();
                foreach (KeyValuePair<string, JToken> rib in (JObject)value)
                {
                    Ribbon _ribbon = JsonConvert.DeserializeObject<Ribbon>(rib.Value.ToString());
                    _ribbons.Add(_ribbon);
                }
            } 
        }
        
        private List<Medal> _medals;
        public object medals { 
            get { return _medals; } 
            set {
                _medals = new List<Medal>();
                foreach (KeyValuePair<string, JToken> m in (JObject)value)
                {
                    Medal _medal = JsonConvert.DeserializeObject<Medal>(m.Value.ToString());
                    _medals.Add(_medal);
                }
            } 
        }
        
        private List<Vehcat> _vehcats;
        public object vehcats { 
            get { return _vehcats; } 
            set {
                _vehcats = new List<Vehcat>();
                foreach(KeyValuePair<string, JToken> vc in (JObject)value) {
                    Vehcat _vehcat = JsonConvert.DeserializeObject<Vehcat>(vc.Value.ToString());
                    _vehcats.Add(_vehcat);
                }
            } 
        }
        
        private List<Kit> _kits;
        public object kits { 
            get { return _kits; } 
            set {
                _kits = new List<Kit>();
                foreach (KeyValuePair<string, JToken> nr in (JObject)value)
                {
                    Kit _kit = JsonConvert.DeserializeObject<Kit>(nr.Value.ToString());
                    _kits.Add(_kit);
                }
            } 
        }
        
        private List<Specialization> _specializations;
        public object specializations { 
            get { return _specializations; } 
            set {
                _specializations = new List<Specialization>();
                foreach (KeyValuePair<string, JToken> sp in (JObject)value)
                {
                    Specialization _spec = JsonConvert.DeserializeObject<Specialization>(sp.Value.ToString());
                    _specializations.Add(_spec);
                }
            } 
        }

        private List<Gamemode> _gamemodes;
        public object gamemodes
        {
            get {
                return _gamemodes;
            } 
            set {
                _gamemodes = new List<Gamemode>();
                foreach (KeyValuePair<string, JToken> gm in (JObject)value)
                {
                    Gamemode _gm = JsonConvert.DeserializeObject<Gamemode>(gm.Value.ToString());
                    _gamemodes.Add(_gm);
                }
            } 
        }

        private List<Level> _coopmissions;
        public object coopmissions { 
            get { return _coopmissions; } 
            set {
                _coopmissions = new List<Level>();
                foreach (KeyValuePair<string, JToken> mission in (JObject)value)
                {
                    Level _lvl = JsonConvert.DeserializeObject<Level>(mission.Value.ToString());
                    _coopmissions.Add(_lvl);
                }
            } 
        }

        private long _wins;
        public long wins
        {
            get
            {
                if (_wins == 0)
                {
                    foreach (Gamemode gm in _gamemodes)
                    {
                        _wins += gm.wins;
                    }
                }
                return _wins;
            }
        }

        private long _losses;
        public long losses
        {
            get
            {
                if (_losses == 0)
                {
                    foreach (Gamemode gm in _gamemodes)
                    {
                        _losses += gm.losses;
                    }
                }
                return _losses;
            }
        }

        private long _totalgames;
        public long totalgames { 
            get {
                if (_totalgames == 0)
                {
                    foreach (Gamemode gm in _gamemodes)
                    {
                        _totalgames += (gm.losses + gm.wins);
                    }
                }
                return _totalgames;
            } 
        }
    }
}
